<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item>
            <el-input v-model="query.params.areaName" placeholder="区域名称" />
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
            <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions" @change="selectTenant" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions" @change="selectOrgan" collapse-tags clearable filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.enable" clearable placeholder="是否生效">
              <el-option v-for="item in query.enableOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-autocomplete v-model="query.params.storeName" :fetch-suggestions="queryStoreList" placeholder="请输所属店铺" @select="handleSelect"></el-autocomplete>
          </el-form-item>
          <el-form-item>
            <el-button v-has="'STORE_AREA_LIST'" type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button v-has="'STORE_AREA_ADD'" t type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="block">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row row-key="id">
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="区域名称" width="110">
          <template slot-scope="scope">
            {{ scope.row.areaName }}
          </template>
        </el-table-column>
        <el-table-column label="所属机构" width="110" prop="organId" :formatter="translateOrgan" />
        <el-table-column label="关联地址" width="110">
          <template slot-scope="scope">
            {{ scope.row.addressId }}
          </template>
        </el-table-column>
        <el-table-column label="是否生效" width="110" prop="enable" :formatter="enableFormat">
        <!--   <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-value="1" active-color="#13ce66" inactive-color="#ff4949">
            </el-switch>
          </template> -->
        </el-table-column>
        <el-table-column label="所属店铺" width="110">
          <template slot-scope="scope">
            {{ scope.row.storeId }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" prop="created_at" label="操作" width="200">
          <template slot-scope="scope">
            <el-link v-has="'STORE_AREA_EDIT'" icon="el-icon-edit" type="warning" style="cursor: pointer;" @click="handleEdit(scope.$index, scope.row)">
              修改
            </el-link>
            <el-link v-has="'STORE_AREA_REMOVE'" icon="el-icon-delete" type="danger" style="cursor: pointer;" @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-link>
            <el-link v-has="'SUB_STORE_AREA_ADD'" icon="el-icon-edit" type="success" style="cursor: pointer;" @click="addSubArea(scope.$index, scope.row)">
              添加子块区
            </el-link>
            <el-link v-has="'STORE_AREA_OF_GOODS_SHELF_ADD'" icon="el-icon-edit" type="success" style="cursor: pointer;" @click="handleQueryGoodsShelf(scope.$index, scope.row)">
              添加货架
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormStoreVisible">
        <div class="block">
          <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column align="center" label="序号" width="95">
              <template slot-scope="scope">
                {{ scope.$index }}
              </template>
            </el-table-column>
            <el-table-column label="所属组织" width="110">
              <template slot-scope="scope">
                {{ scope.row.organId }}
              </template>
            </el-table-column>
            <el-table-column label="组织路径" width="110">
              <template slot-scope="scope">
                {{ scope.row.organPath }}
              </template>
            </el-table-column>
            <el-table-column label="店铺名称" width="110">
              <template slot-scope="scope">
                {{ scope.row.storeName }}
              </template>
            </el-table-column>
            <el-table-column label="是否生效" width="110" prop="enable" :formatter="enableFormat" />
            <el-table-column label="纬度" width="110">
              <template slot-scope="scope">
                {{ scope.row.latitude }}
              </template>
            </el-table-column>
            <el-table-column label="经度" width="110">
              <template slot-scope="scope">
                {{ scope.row.longitude }}
              </template>
            </el-table-column>
            <el-table-column label="标注" width="110">
              <template slot-scope="scope">
                {{ scope.row.remark }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" prop="created_at" label="操作">
              <template slot-scope="scope">
                <span class="el-tag el-tag--info el-tag--mini" style="cursor: pointer;" @click="handleEdit(scope.$index, scope.row)">
                  {{ scope.row.isSet?'保存':"修改" }}
                </span>
                <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;" @click="handleDelete(scope.$index, scope.row)">
                  删除
                </span>
                <span v-else class="el-tag  el-tag--mini" style="cursor: pointer;" @click="pwdChange(scope.row,scope.$index,false)">
                  取消
                </span>
                <el-link v-has="'ORGAN_EDIT'" icon="el-icon-edit" type="warning" style="cursor: pointer;" @click="handleStoreAreaEdit(scope.$index, scope.row)">
                  块区管理
                </el-link>
                <el-divider v-has="'ORGAN_REMOVE'" direction="vertical"></el-divider>
                <el-link v-has="'ORGAN_EDIT'" icon="el-icon-circle-plus" type="warning" style="cursor: pointer;" @click="handleStoreGoodsEdit(scope.$index, scope.row)">
                  添加商品
                </el-link>
                <el-divider v-has="'ORGAN_REMOVE'" direction="vertical"></el-divider>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="block">
          <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogStoreListVisible">
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogStoreListVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleChoseStore">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="'添加货架'" width="88%" :visible.sync="dialogGoodsShelfVisible">
        <div class="Search">
          <!--工具条-->
          <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
              <el-form-item>
                <el-cascader ref="organTreeShelf" placeholder="所属组织" :props="props" :options="organOptions" @change="selectShelfOrgan" collapse-tags filterable></el-cascader>
              </el-form-item>
              <el-form-item v-if="false">
                <el-input v-model="query.goodsShelf.areaId" hidden placeholder="所属区域" />
              </el-form-item>
              <el-form-item>
                <el-input v-model="query.goodsShelf.shelfName" placeholder="货架名称" />
              </el-form-item>
              <el-form-item>
                <el-select v-model="query.goodsShelf.enable" placeholder="是否生效">
                  <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-input v-model="query.goodsShelf.addressId" placeholder="所属地址" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryGoodsShelf">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAddGoodsShelf">新增</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </div>
        <div class="block">
          <el-table v-loading="listLoading" :data="goodsShelfList" element-loading-text="Loading" border fit highlight-current-row>
            <el-table-column align="center" label="序号" width="95">
              <template slot-scope="scope">
                {{ scope.$index }}
              </template>
            </el-table-column>
            <el-table-column label="货架名称" width="110">
              <template slot-scope="scope">
                {{ scope.row.shelfName }}
              </template>
            </el-table-column>
            <el-table-column label="所属组织" width="110">
              <template slot-scope="scope">
                {{ scope.row.organId }}
              </template>
            </el-table-column>
            <el-table-column label="所属块区" width="110">
              <template slot-scope="scope">
                {{ scope.row.areaId }}
              </template>
            </el-table-column>
            <el-table-column label="排序字段" width="110">
              <template slot-scope="scope">
                {{ scope.row.sorted }}
              </template>
            </el-table-column>
            <el-table-column label="是否生效" width="110">
              <template slot-scope="scope">
                {{ scope.row.enable }}
              </template>
            </el-table-column>
            <el-table-column label="所属地址" width="110">
              <template slot-scope="scope">
                {{ scope.row.addressId }}
              </template>
            </el-table-column>
            <!-- <el-table-column label="生成者" width="110">
              <template slot-scope="scope">
                {{ scope.row.createBy }}
              </template>
            </el-table-column>
            <el-table-column label="生成时间" width="110">
              <template slot-scope="scope">
                {{ scope.row.createDate }}
              </template>
            </el-table-column>
            <el-table-column label="更新者" width="110">
              <template slot-scope="scope">
                {{ scope.row.updateBy }}
              </template>
            </el-table-column>
            <el-table-column label="更新时间" width="110">
              <template slot-scope="scope">
                {{ scope.row.updateDate }}
              </template>
            </el-table-column> -->
            <el-table-column label="备注" width="110">
              <template slot-scope="scope">
                {{ scope.row.remark }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" prop="created_at" label="操作" width="200">
              <template slot-scope="scope">
                <el-link v-has="'GOODS_SHELF_EDIT'" icon="el-icon-edit" type="warning" style="cursor: pointer;" @click="handleGoodsShelfEdit(scope.$index, scope.row)">
                  修改
                </el-link>
                <el-link v-has="'GOODS_SHELF_REMOVE'" icon="el-icon-edit" type="danger" style="cursor: pointer;" @click="handleGoodsShelfDelete(scope.$index, scope.row)">
                  删除
                </el-link>
                <el-link v-has="'GOODS_ON_SHELF_ADD'" icon="el-icon-plus" type="success" style="cursor: pointer;" @click="handleGoodsPriceOnSale(scope.$index, scope.row)">
                  商品上架
                </el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="block">
          <el-pagination :current-page="query.page.goodsShelf.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="query.page.goodsShelf.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="query.page.goodsShelf.total" @size-change="handleGoodsShelfSizeChange" @current-change="handleGoodsShelfCurrentChange" />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogGoodsShelfVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item v-if="addForVisible">
            <el-input v-model="form.id" disabled auto-complete="off" />
          </el-form-item>
          <el-form-item label="所属店铺">
            <el-input v-model="form.storeId" disabled placeholder="所属店铺" auto-complete="off" />
          </el-form-item>
          <el-form-item label="区域名称">
            <el-input v-model="form.areaName" placeholder="区域名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="是否生效">
            <el-select v-model="form.enable" clearable placeholder="是否生效" style="width: 100%">
              <el-option v-for="item in query.enableOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="form.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps" :options="tenantOptions" @change="chooseTenantForm" style="width:100%" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item v-has="'STORE_AREA_ORGAN_MGR'" label="所属组织">
            <el-cascader ref="editOrganTree" placeholder="所属组织" :props="props" :options="organOptions" @change="chooseOrgan" collapse-tags filterable style="width: 100%"></el-cascader>
          </el-form-item>
          <el-form-item label="所属父区域">
            <el-input v-model="form.parentId" disabled placeholder="所属父区域" auto-complete="off" />
          </el-form-item>
          <el-form-item label="关联地址">
            <el-input v-model="form.addressId" placeholder="关联地址" auto-complete="off" />
          </el-form-item>
          <el-form-item label="标注">
            <el-input v-model="form.remark" placeholder="标注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogAddGoodsShelfVisible">
        <el-form :model="form">
          <el-form-item v-if="addForVisible" label="货架编号">
            <el-input v-model="goodsShelf.id" auto-complete="off" />
          </el-form-item>
          <el-form-item label="所属组织" hidden>
            <el-input v-model="goodsShelf.organId" placeholder="所属组织" auto-complete="off" />
          </el-form-item>
          <el-form-item label="所属区域">
            <el-input v-model="goodsShelf.areaId" disabled placeholder="所属区域" auto-complete="off" />
          </el-form-item>
          <el-form-item label="父区域" hidden>
            <el-input v-model="goodsShelf.parentId" placeholder="父区域" auto-complete="off" />
          </el-form-item>
          <el-form-item label="货架名称">
            <el-input v-model="goodsShelf.shelfName" placeholder="货架名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="所属地址" hidden>
            <el-input v-model="goodsShelf.addressId" placeholder="所属地址" auto-complete="off" />
          </el-form-item>
          <el-form-item label="是否生效">
            <el-select v-model="goodsShelf.enable" placeholder="是否生效" style="width:100%">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序字段">
            <el-input v-model="goodsShelf.sorted" placeholder="排序字段" auto-complete="off" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="goodsShelf.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddGoodsShelfVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleGoodsShelfSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getList, doDelete, doEdit, queryAreaOfStore } from '@/api/StoreArea/'
import { getList as getStoreList } from '@/api/Store/'
import { getDictionaryGroup } from '@/api/dictionary'
import { getList as getGoodsShelfList, doDelete as deleteGoodsShelf, doEdit as editGoodsShelf } from '@/api/GoodsShelf/'
import { getList as getOrganPager, querySubordinateOrgan } from '@/api/Organ'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject, getSystemErrorCode } from '@/api/utils'
import { getTenantList, getOrganList, getDictionary, translateOrganId2OrganName } from '@/utils/auth'
import localStorageUtil from '@/utils/localStorageUtil'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(status) {
      const typeMap = {
        '1': '超级管理员',
        '2': '管理员',
        '3': '普通用户'
      }
      const typeDesc = typeMap[status]
      return typeDesc
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      dialogFormStoreVisible: false,
      dialogStoreListVisible: false,
      dialogGoodsShelfVisible: false,
      dialogAddGoodsShelfVisible: false,
      goodsShelfList: [],
      form: {
        addressId: '',
        areaName: '',
        createBy: '',
        createDate: '',
        enable: '',
        id: '',
        organId: '',
        organPath: '',
        parentId: '',
        remark: '',
        sorted: '',
        storeId: '',
        updateBy: '',
        updateDate: ''
      },
      goodsShelf: {
        id: '',
        organId: '',
        areaId: '',
        parentId: '',
        shelfName: '',
        sorted: '',
        enable: '',
        addressId: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑',
        addSubArea: '添加子块区'
      },
      props: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '组织',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      tenantProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      // 新增和编辑弹框标题
      dialogStatus: '',
      query: {
        params: {
          addressId: '', // 关联地址
          areaName: '', // 区域名称
          createBy: '', // 添加者
          createDate: '', // 生成时间
          enable: '', // 是否生效
          id: '', // 主键
          organId: '', // 所属组织
          organPath: '', // 组织路径
          parentId: '', // 所属父区域
          remark: '', // 标注
          sorted: '', // 排序字段
          storeId: '', // 所属店铺
          updateBy: '', // 更新者
          updateDate: '' // 更新时间

        },
        store: {
          addressId: '', // 关联地址
          createBy: '', // 添加者
          createDate: '', // 新增时间
          enable: '', // 是否生效
          id: '', // 主键
          latitude: '', // 纬度
          longitude: '', // 经度
          organId: '', // 所属组织
          organPath: '', // 组织路径
          remark: '', // 标注
          storeName: '', // 店铺名称
          updateBy: '', // 更新者
          updateDate: '', // 更新时间
          total: 0,
          pageSize: 10,
          currentPage: 1
        },
        goodsShelf: {
          id: '',
          organId: '',
          areaId: '',
          parentId: '',
          shelfName: '',
          sorted: '',
          enable: '',
          addressId: '',
          isDeleted: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remark: ''
        },
        page: {
          goodsShelf: {
            total: 0,
            pageSize: 10,
            currentPage: 1
          }
        },
        timeValue: '',
        enableOptions: [{
          value: '0',
          label: '失效'
        }, {
          value: '1',
          label: '生效'
        }],
        storeOption: [],
        value: ''
      }
    }
  },
  created() {
    this.initOrganTree(() => {
      this.setOrganQueryType()
    }, getOrganPath())
    this.initOrganFormTree()
    this.handleFiltTenant()
    this.handleBuildEnableOptions()
    this.fetchData()
  },
  methods: {
    translateOrgan(row) {
      return translateOrganId2OrganName(row);
    },
    enableFormat(row, column) {

      let label = ''
      for (let i = 0; i < this.query.enableOptions.length; i++) {
        if ('' + row.enable === this.query.enableOptions[i].value) {
          return this.query.enableOptions[i].label
        }
      }
      return label
    },
    fetchData() {
      this.listLoading = true
      this.createOrganQuery()
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data

        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        } else {
          this.total = data.totalCount
          this.pageSize = data.pageSize
          this.currentPage = data.currentPage
          this.list = handleBuildTreeDataStruct(data.entities, 'id', 'parentId')
        }
        this.listLoading = false
      })
    },
    createOrganQuery() {
      // 选中了租户
      let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
      let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
      let that = this
      this.query.params.organId = null
      if (tenantSelected.length && tenantSelected[0]) {
        this.query.params.tenantId = tenantSelected[0].data.id
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
          }
        }
      } else {
        this.query.params.tenantId = getTenantId()
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0].data.organPath
          }
        }

      }
      this.query.params.organId = this.query.params.organId || getOrganId()
      this.query.params.organPath = this.query.params.organPath || getOrganPath()
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.addressId = ''
      this.form.areaName = ''
      this.form.createBy = ''
      this.form.createDate = ''
      this.form.enable = ''
      this.form.id = ''
      this.form.organId = ''
      this.form.organPath = ''
      this.form.parentId = ''
      this.form.remark = ''
      this.form.sorted = ''
      this.form.storeId = ''
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)

      this.form.addressId = row.addressId
      this.form.areaName = row.areaName
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.enable = row.enable
      this.form.id = row.id
      this.form.organId = row.organId
      this.form.organPath = row.organPath
      this.form.parentId = row.parentId
      this.form.remark = row.remark
      this.form.sorted = row.sorted
      this.form.storeId = row.storeId
      this.form.updateBy = row.updateBy
      this.form.updateDate = row.updateDate

      this.addForVisible = true

      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleDelete(index, row) {
      console.log(index)
      console.log(row)

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doDelete(row.id).then(response => {
          const data = response.data
          console.log(data)
          if (data.status === 1) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData()
          } else if (data.status === 403) {
            this.$message({
              type: 'warning',
              message: data.description
            })
          } else {
            this.$message({
              type: 'info',
              message: '操作失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      debugger

      doEdit(this.form).then(response => {
        debugger
        console.log(response)
        const data = response.data
        if (data.status) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    addSubArea(index,row) {
      debugger
      this.form.parentId = row.id
      // 显示弹框
      // 新增弹框标题
      this.dialogStatus = 'addSubArea'
      this.form.addressId = ''
      this.form.areaName = ''
      this.form.createBy =  getCurrentLoginUserId()
      this.form.createDate = ''
      this.form.enable = ''
      this.form.id = ''
      this.form.isDeleted = false
      this.form.tenantId = row.tenantId || getTenantId()
      this.form.organId = row.organId || getOrganId()
      this.form.organPath =  row.organPath || getOrganPath()
      this.form.remark = ''
      this.form.sorted = ''
      this.form.storeId = row.storeId
      this.form.updateBy = getCurrentLoginUserId()
      this.form.updateDate = ''
      this.addForVisible = false
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    initOrganTree(fn, filter, that) {
      this.listLoading = true
      let data = getOrganList()
      if (filter) {
        data = data.filter(function(item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
        that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      } else {
        this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      }
      this.listLoading = false
      if (fn) {
        fn()
      }
    },
    initOrganFormTree(fn, filter) {
      this.initOrganTree(fn, filter, this)
    },
    setOrganQueryType() {
      this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      let that = this
      this.query.params.organPath = ''
      this.query.params.organIds = []
      if (this.props.multiple) {
        this.query.params.organIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function(element) {
            that.query.params.organIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.organPath = nodesInfo[0].data.organPath
      }
    },
    selectTenant(nodes) {
      debugger
      let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
      let that = this
      this.query.params.tenantId = ''
      this.query.params.tenantIds = []
      this.query.params.organId = null
      this.query.params.organIds = []
      this.query.params.organPath = null

      if (this.tenantProps.multiple) {
        this.query.params.tenantIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function(element) {
            that.query.params.tenantIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.tenantId = nodesInfo[0].data.id
        this.initOrganTree(function() {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath)
      } else {
        this.initOrganTree(function() {
          that.setOrganQueryType()
        })
      }
    },
    chooseTenantForm(nodes) {
      let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
      this.form.tenantId = null
      let that = this
      debugger
      if (nodesInfo.length) {
        this.form.tenantId = nodesInfo[0].data.id
        this.initOrganFormTree(function() {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath, this.organFormOptions)
      } else {
        this.initOrganFormTree(function() {
          that.setOrganQueryType()
        }, null, this.organFormOptions)
      }
    },
    organQueryModelChange(organQueryModel) {
      if (organQueryModel == 'selected') {
        this.props.multiple = true
      } else if (organQueryModel == 'selfAndSub') {
        this.props.multiple = false
      }
    },
    chooseOrganForm(nodes) {
      let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
      if (nodesInfo[0]) {
        this.form.organPath = nodesInfo[0].data.organPath
        this.form.organId = nodesInfo[0].data.id
        this.form.tenantId = nodesInfo[0].data.tenantId
      }
    },
    handleFiltTenant() {
      let data = getOrganList()
      this.tenantOptions = data.filter(function(item, index) {
        return item.type == 0 || item.type == 1;
      })
    },
    queryStoreList(queryString, cb) {

      this.listLoading = true
      this.query.store.organPath = getOrganPath();

      getStoreList(this.query.store, this.query.store.currentPage, this.query.store.pageSize).then(response => {
        const data = response.data
        this.query.store.total = data.totalCount
        this.query.store.pageSize = data.pageSize
        this.query.store.currentPage = data.currentPage
        let storeList = []
        data.entities.forEach(function(item) {
          storeList.push({ value: item.storeName })
        })
        cb(storeList)
        this.listLoading = false
      })

    },
    handleChoseStore() {
      this.storeOption = [{
        value: '11111111',
        label: '店铺1'
      }]
    },
    handleSelect(item) {
      console.log(item);
    },
    handleQueryGoodsShelf( index, row) {
      this.dialogGoodsShelfVisible = true
      this.listLoading = true
      if (row && row.id) {
        this.query.goodsShelf.areaId = row.id
      }
      debugger
      getGoodsShelfList(this.query.goodsShelf, this.query.page.goodsShelf.currentPage, this.query.page.goodsShelf.pageSize).then(response => {
        const data = response.data
        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
          return;
        }

        this.query.page.goodsShelf.total = data.totalCount
        this.query.page.goodsShelf.pageSize = data.pageSize
        this.query.page.goodsShelf.currentPage = data.currentPage
        let storeList = []
        this.goodsShelfList = data.entities
        this.listLoading = false
      })
    },
    handleAddGoodsShelf() {
      this.dialogAddGoodsShelfVisible = true
      this.goodsShelf.organId = getOrganId()
      this.goodsShelf.organPath = getOrganPath()
      this.goodsShelf.areaId = this.query.goodsShelf.areaId
    },
    handleGoodsShelfSave() {

      this.goodsShelf.isDeleted = false
      editGoodsShelf(this.goodsShelf).then(response => {
        const data = response.data
        if (data.status) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.handleQueryGoodsShelf(null, null);
      })
    },
    handleGoodsShelfSizeChange() {

    },
    handleGoodsShelfCurrentChange() {

    },
    chooseOrgan(nodes) {
      let nodesInfo = this.$refs['editOrganTree'].getCheckedNodes()
      this.form.organPath = nodesInfo[0].data.organPath
    },
    selectShelfOrgan(nodes) {
      let nodesInfo = this.$refs['organTreeShelf'].getCheckedNodes()
      this.query.goodsShelf.organPath = nodesInfo[0].data.organPath
    },
     handleBuildEnableOptions() {

      const datas = getDictionary()
      let data = datas.filter(function(item, index) {
        return item.bigType === "UserEnable" && item.parentId != '0';
      })
      let enableOptions = [];
      for (let i = 0; i < data.length; i++) {

        let option = {}
        option['value'] = data[i].value
        option['label'] = data[i].desc
        enableOptions.push(option)
      }
      this.query.enableOptions = enableOptions
    },
  }
}

</script>
